package net.http;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.util.*;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/10/19
 * @Version：1.0.0
 */
public class HttpTest {

    private static final Logger logger = LoggerFactory.getLogger(String.valueOf(HttpTest.class));

    private static int count = 0;

    public static void main(String[] args) {

        new Timer("testTimer").schedule(new TimerTask() {
            @Override
            public void run() {
                String url = getUrl();
                logger.info("定时任务开始执行======");
                doPost(url);
            }
        }, 5000, 5000);

        logger.info("结束执行==========");
    }

    private static String getUrl() {
        List<String> list = Arrays.asList("https://editor.csdn.net/md/?articleId=112207741",
                "https://blog.csdn.net/qq_37248504/article/details/111937612",
                "https://blog.csdn.net/qq_37248504/article/details/111767707",
                "https://blog.csdn.net/qq_37248504/article/details/111600769",
                "https://blog.csdn.net/qq_37248504/article/details/123078361",
                "https://blog.csdn.net/qq_37248504/article/details/123509139",
                "https://blog.csdn.net/qq_37248504/article/details/122991309",
                "https://blog.csdn.net/qq_37248504/article/details/122546818",
                "https://blog.csdn.net/qq_37248504/article/details/122418907",
                "https://blog.csdn.net/qq_37248504/article/details/122147435",
                "https://blog.csdn.net/qq_37248504/article/details/121985401",
                "https://blog.csdn.net/qq_37248504/article/details/121479162",
                "https://blog.csdn.net/qq_37248504/article/details/121050976",
                "https://blog.csdn.net/qq_37248504/article/details/120790348",
                "https://blog.csdn.net/qq_37248504/article/details/120635184",
                "https://blog.csdn.net/qq_37248504/article/details/128722785?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/128434248?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/128193608?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/128105605?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/127938165?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/127762717?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/127623050?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/127349776?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/127192541?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/126965385?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/126686726?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/126412980?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/126412947?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/126259099?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125982589?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125750963?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125666929?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125608959?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125460410?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125383986?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/125018261?spm=1001.2014.3001.5502",
                "https://blog.csdn.net/qq_37248504/article/details/120120258",
                "https://blog.csdn.net/qq_37248504/article/details/119980813",
                "https://blog.csdn.net/qq_37248504/article/details/119844560",
                "https://blog.csdn.net/qq_37248504/article/details/119517816",
                "https://blog.csdn.net/qq_37248504/article/details/119303463",
                "https://blog.csdn.net/qq_37248504/article/details/119088250",
                "https://blog.csdn.net/qq_37248504/article/details/118605663",
                "https://blog.csdn.net/qq_37248504/article/details/118367963",
                "https://blog.csdn.net/qq_37248504/article/details/116171452",
                "https://blog.csdn.net/qq_37248504/article/details/118580934",
                "https://blog.csdn.net/qq_37248504/article/details/116894933",
                "https://blog.csdn.net/qq_37248504/article/details/115269995",
                "https://blog.csdn.net/qq_37248504/article/details/115266913",
                "https://blog.csdn.net/qq_37248504/article/details/113574720",
                "https://blog.csdn.net/qq_37248504/article/details/113274412",
                "https://blog.csdn.net/qq_37248504/article/details/112972378",
                "https://blog.csdn.net/qq_37248504/article/details/105590492",
                "https://blog.csdn.net/qq_37248504/article/details/109135995",
                "https://blog.csdn.net/qq_37248504/article/details/110733985",
                "https://blog.csdn.net/qq_37248504/article/details/107009798",
                "https://blog.csdn.net/qq_37248504/article/details/106932085",
                "https://blog.csdn.net/qq_37248504/article/details/106773321",
                "https://blog.csdn.net/qq_37248504/article/details/106740467",
                "https://blog.csdn.net/qq_37248504/article/details/110728398",
                "https://blog.csdn.net/qq_37248504/article/details/110502016",
                "https://blog.csdn.net/qq_37248504/article/details/110007064",
                "https://blog.csdn.net/qq_37248504/article/details/109861871",
                "https://blog.csdn.net/qq_37248504/article/details/123293585",
                "https://blog.csdn.net/qq_37248504/article/details/115386752",
                "https://blog.csdn.net/qq_37248504/article/details/109861854",
                "https://blog.csdn.net/qq_37248504/article/details/109606523");
        int size = list.size();
        Random random = new Random();
        int i = random.nextInt(size);
        String s = list.get(i);
        logger.info("得到的Url为" + s);
        return s;
    }


    public static void doPost(String str) {
        BufferedReader bufferedReader = null;
        try {
            // HttpClient client = new DefaultHttpClient(); //构建一个Client
            HttpClient client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).build();
            HttpPost post = new HttpPost(str);//构建一个POST请求
            // 构建表单参数
            List<NameValuePair> formParams = new ArrayList<>();
            // formParams.add(new BasicNameValuePair("username", "yourname"));
            // formParams.add(new BasicNameValuePair("password", "yourpassword"));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");//将表单参数转化为“实体”
            post.setEntity(entity);//将“实体“设置到POST请求里

            RequestConfig defaultConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
            post.setConfig(defaultConfig);
            HttpResponse response = client.execute(post);//提交POST请求
            logger.info("请求成功！");
            count++;
            logger.info("=====================================" + count);

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }


    }


}
